﻿//----------------------------------------------------------------------------------------------------
// The ResourceManager is responsible for tracking and distributing resources
//----------------------------------------------------------------------------------------------------

package HG.Managers
{
	import HG.GameService;
	
	import HG.Events.ResourceEvent;
	import HG.Events.NodeEvent;
	import HG.Events.PlayerEvent;
	import HG.Events.TurnEvent;
	
	import HG.Managers.LevelManager;
	import HG.Managers.PlayerManager;
	import HG.Managers.TurnManager;
	
	public class ResourceManager extends IGameManager
	{
		var m_levelManager:LevelManager;
		var m_playerManager:PlayerManager;
		var m_turnManager:TurnManager;
		
		var m_rEvt:ResourceEvent;
		
		var m_rateMap:Array;
		var m_valueMap:Array;
		
		//----------------------------------------------------------------------------------------------------
		public function ResourceManager()
		{
			m_rateMap = new Array();
			m_valueMap = new Array();
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Initialize()
		{
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			m_turnManager = GameService.GetInstance().GetTurnManager();
			
			m_levelManager.addEventListener(NodeEvent.CAPTURE, OnNodeCaptured);
			m_playerManager.addEventListener(PlayerEvent.JOIN, OnPlayerJoin);
			m_turnManager.addEventListener(TurnEvent.BEGIN, OnTurnBegin);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public function CanPlayerAfford(playerNumber:Number, cost:Number):Boolean
		{
			if (m_valueMap[playerNumber] >= cost)
				return true;
				
			return false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SpendResource(playerNumber:Number, cost:Number)
		{
			m_valueMap[playerNumber] -= cost;
			RaiseResourceUpdatedEvent(playerNumber);
		}
		
		//----------------------------------------------------------------------------------------------------
		// Events
		//----------------------------------------------------------------------------------------------------
		private function RaiseResourceUpdatedEvent(playerNumber:Number)
		{
			m_rEvt = new ResourceEvent(ResourceEvent.UPDATE);
			m_rEvt.m_playerNumber = playerNumber;
			m_rEvt.m_resourceValue = m_valueMap[playerNumber];
			m_rEvt.m_resourceRate = m_rateMap[playerNumber];
			dispatchEvent(m_rEvt);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function RaiseRateChangeEvent(playerNumber:Number)
		{
			m_rEvt = new ResourceEvent(ResourceEvent.RATE_CHANGE);
			m_rEvt.m_playerNumber = playerNumber;
			m_rEvt.m_resourceValue = m_valueMap[playerNumber];
			m_rEvt.m_resourceRate = m_rateMap[playerNumber];
			dispatchEvent(m_rEvt);
		}
		
		//----------------------------------------------------------------------------------------------------
		// External events
		//----------------------------------------------------------------------------------------------------
		public function OnNodeCaptured(evt:NodeEvent)
		{
			if (evt.m_node.GetType() == "Generator" ||
				evt.m_node.GetType() == "Root")
			{
				// TODO: deal with multiple owners, somehow...
				m_rateMap[evt.m_capBy] += 1;
				
				RaiseRateChangeEvent(evt.m_capBy);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function OnPlayerJoin(evt:PlayerEvent)
		{
			m_rateMap[evt.m_playerNumber] = 0;
			m_valueMap[evt.m_playerNumber] = 1;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function OnTurnBegin(evt:TurnEvent)
		{
			var index:Number = 0;
			var listLength:Number = m_rateMap.length;
			
			// index 0 = environemnt
			for (index = 1; index < listLength; ++index)
			{
				m_valueMap[index] += m_rateMap[index];
				RaiseResourceUpdatedEvent(index);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}